
layui.form.render('select','myDiv');

userList(1);

function userList(current) {

    var nickName = $("#nickName").val();

    var level = $("#level option:selected").val();

    //调用ajax
    var settings = {
        "url": base_url+"/admin/base?action=userList&current="+current+"&size=14&nickName="+nickName+"&level="+level,
        "method": "GET",
        "timeout": 0,
        "headers": {
            "token": ''
        }
    };

    $(function () {
        $.ajax(settings).done(function (response) {
            console.log(response);
            if (response.code == 403) {
                layer.alert(response.message);
            } else if (response.code == 200) {

                var total = response.data.total;
                var current = response.data.current;

                //渲染数据
                var records = response.data.records;
                $("#users").html("");
                for (var i = 0; i < records.length; i++) {

                    $("#users").append('    <tr onclick="">\n' +
                        '      <td>'+records[i].userName+'</td>\n' +
                        '      <td>'+records[i].nickName+'</td>\n' +
                        '      <td><img style="width: 30px;height: 30px" src="'+records[i].headUrl+'"/></td>\n' +
                        '      <td><img style="width: 30px;height: 30px" src="'+records[i].levelIcon+'"/><p>'+records[i].levelName+'</p></td>\n' +
                        '      <td>'+records[i].payStone+'</td>\n' +
                        '      <td>' +
                        '<button type="button" class="layui-btn layui-btn-normal" onclick="info('+records[i].id+')">查看人物</button>' +
                        '<button type="button" class="layui-btn layui-btn-normal" onclick="toUser('+records[i].id+')">免登陆</button>' +
                        '</td>\n' +
                        '    </tr>');
                }

                PageInit(current,total);

            }else{
                layer.alert("服务器开小差...");
            }



        });
    });



}

//分页控件
function PageInit(curr, RecordCount) {

    //分页
    laypage.render({
        elem: 'userPage' //分页容器的id
        ,count: RecordCount //数据总数
        ,limit: 14//每页显示的数据条数
        ,curr: curr
        ,skin: '#1E9FFF' //自定义选中色值
        ,layout: ['prev', 'page', 'next', 'count', 'limit', 'refresh', 'skip'] //自定义排版
        ,jump: function(obj, first){
            if(!first){
                //layer.msg('第'+ obj.curr +'页', {offset: 'b'});
                userList(obj.curr);
            }
        }
    });

}

//查询人物数据
function info(val) {

    //调用ajax
    var settings = {
        "url": base_url+"/api/base?action=getUserInfo&id="+val,
        "method": "GET",
        "timeout": 0,
        "headers": {
            "token": ''
        }
    };

    $(function () {
        $.ajax(settings).done(function (response) {
            console.log(response);
            if (response.code == 403) {
                layer.alert(response.message);
            } else if (response.code == 200) {

                var user =response.data;

                layer.open({
                    type: 1
                    ,title: false //不显示标题栏
                    ,closeBtn: false
                    ,area: '300px;'
                    ,shade: 0.8
                    ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
                    ,btn: ['查看人物', '关闭人物']
                    ,btnAlign: 'c'
                    ,moveType: 1 //拖拽模式，0或者1
                    ,content: '<div id="character" style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">' +
                        '<div>头像:</div><span><img width="50" height="50" src="'+user.head_url+'" alt=""/></span><br/>'+
                        '<div>昵称:</div><span>'+user.nick_name+'</span><br/>'+
                        '<div>级别:</div><span><img style="width: 30px;height: 30px" src="'+user.level_icon+'"/>'+user.level_name+'</span><br/>'+
                        '<div>用户名:</div><span>'+user.user_name+'</span><br/>'+
                        '<div>血量:</div><span>'+user.character_hp+'/'+user.character_hp+'</span><br/>'+
                        '<div>蓝量:</div><span>'+user.character_mp+'/'+user.character_mp+'</span><br/>'+
                        '<div>怒气:</div><span>'+user.character_re+'/'+user.character_re+'</span><br/>'+
                        '<div>攻击:</div><span>'+user.aggressivity+'</span><br/>'+
                        '<div>防御:</div><span>'+user.defense+'</span><br/>'+
                        '<div>灵石:</div><span>'+user.pay_stone+'</span><br/>'+
                        '<div>经验:</div><span>'+user.experience_num+'/'+user.ec_required+'</span>'+
                        '<div>进度条:</div><span>' +
                        '<div class="layui-progress" lay-filter="demo" lay-showPercent="yes">\n' +
                        '  <div class="layui-progress-bar layui-bg-green" lay-percent="30%"></div>\n' +
                        '</div>' +
                        '</span>'+
                        '</div>'
                    ,success: function(layero){
                        var btn = layero.find('.layui-layer-btn');
                        btn.find('.layui-layer-btn0').attr({
                            href: 'http://www.layui.com/'
                            ,target: '_blank'
                        });
                    }
                });

                var percent = parseInt(user.experience_num)/parseInt(user.ec_required)*100;

                element.progress("demo",percent+'%');

            }else{
                layer.alert("服务器开小差...");
            }

        });
    });

}

//免登陆跳转用户界面
function toUser(val) {
    location.href = base_url+"/user/"+val;
}
